/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javalight.network.ro.server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import org.xsocket.datagram.Endpoint;
import org.xsocket.datagram.IDatagramHandler;
import org.xsocket.datagram.IEndpoint;
import org.xsocket.datagram.UserDatagram;

/**
 *
 * @author Ruby
 */
public class DataServer {

	static Logger logger = Logger.getLogger("");
    public static void main(String... arg) throws IOException
    {
    	//new Server(10190, new DefaultDataHandler()).run();
    	//Endpoint server = new Endpoint(1024*10, new Handler(), Executors.newCachedThreadPool());
    	
    	DatagramSocket server = new DatagramSocket(10190);
    	byte[] buf = new byte[1024*10];
    	DatagramPacket packet = new DatagramPacket(buf, buf.length);
    	server.receive(packet);
    	System.out.println(new String(buf, 0, packet.getLength()));
    }
}

class Handler implements IDatagramHandler
{

	@Override
	public boolean onDatagram(IEndpoint end) throws IOException {
		UserDatagram datagram = end.receive(0);
		byte[] lastReceived = datagram.readBytes();
		UserDatagram response = new UserDatagram(datagram.getRemoteSocketAddress(), lastReceived);
		end.send(response);
		return false;
	}
	
}
